<?php
// 检查数据库状态的简单脚本

// 包含数据库配置
$config = include 'application/database.php';

try {
    $dsn = 'mysql:host=' . $config['hostname'] . ';dbname=' . $config['database'] . ';charset=' . $config['charset'];
    $pdo = new PDO($dsn, $config['username'], $config['password']);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    echo "=== 数据库连接成功 ===\n\n";
    
    // 统计总菜品数
    $stmt = $pdo->query('SELECT COUNT(*) as total FROM fa_food_dish');
    $total = $stmt->fetch(PDO::FETCH_ASSOC);
    echo "总菜品数: " . $total['total'] . "\n";
    
    // 统计已处理菜品数
    $stmt = $pdo->query('SELECT COUNT(*) as processed FROM fa_food_dish WHERE ai_called = 1');
    $processed = $stmt->fetch(PDO::FETCH_ASSOC);
    echo "已处理菜品数: " . $processed['processed'] . "\n";
    
    // 统计未处理菜品数
    $stmt = $pdo->query('SELECT COUNT(*) as unprocessed FROM fa_food_dish WHERE ai_called = 0');
    $unprocessed = $stmt->fetch(PDO::FETCH_ASSOC);
    echo "未处理菜品数: " . $unprocessed['processed'] . "\n";
    
    // 显示最近处理的5个菜品
    echo "\n=== 最近处理的5个菜品 ===\n";
    $stmt = $pdo->query('SELECT id, name, cook_time, steps FROM fa_food_dish WHERE ai_called = 1 ORDER BY updatetime DESC LIMIT 5');
    $dishes = $stmt->fetchAll(PDO::FETCH_ASSOC);
    foreach ($dishes as $dish) {
        echo "ID: " . $dish['id'] . ", 名称: " . $dish['name'] . "\n";
        echo "  烹饪时长: " . $dish['cook_time'] . "\n";
        echo "  制作步骤: " . substr($dish['steps'], 0, 50) . "...\n\n";
    }
    
    // 显示最近的食材关联数据
    echo "=== 最近的食材关联数据 ===\n";
    $stmt = $pdo->query('SELECT r.id, r.dish_id, d.name as dish_name, r.ingredient_name, r.quantity, r.ingredient_id FROM fa_food_ingredientrel r LEFT JOIN fa_food_dish d ON r.dish_id = d.id ORDER BY r.createtime DESC LIMIT 5');
    $relations = $stmt->fetchAll(PDO::FETCH_ASSOC);
    foreach ($relations as $rel) {
        echo "关联ID: " . $rel['id'] . ", 菜品: " . $rel['dish_name'] . " (" . $rel['dish_id'] . ")\n";
        echo "  食材: " . $rel['ingredient_name'] . ", 用量: " . $rel['quantity'] . ", 食材ID: " . $rel['ingredient_id'] . "\n\n";
    }
    
} catch (Exception $e) {
    echo "数据库连接失败: " . $e->getMessage() . "\n";
}
?>